/*
 * Created on 11.06.2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package de.localsport.util;


import java.util.*;
import de.localsport.util.Team;


/**
 * @author Stephan
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Ranking {

	String getTableForGroup (String group){
    
		java.sql.Connection conn1 = null;
		java.sql.Statement stat1 = null;

		List TeamList = Collections.synchronizedList(new ArrayList());  
    
		int iValue = 0;
    
		try {
		  conn1 = null; //cn();
		  stat1 = conn1.createStatement();
		  java.sql.ResultSet rs = null; //openrs( stat1, "SELECT home, guest, home_score, guest_score FROM matches WHERE tgroup='" + group + "' AND match_type = 0");
      
		  //Gruppe existiert nicht.
		  if (! rs.next()) {
			rs.close();
			stat1.close();
			conn1.close();
			return "";
		  }
   	
		  //Alle Heim-Mannschaften einlesen
		  while (rs.next()) {
		boolean teamExists = false;   	
		Team tmpTeam = new Team(rs.getInt("home"));
   		
		for (Iterator it = TeamList.iterator(); it.hasNext();){      	  
				if ( ((Team)it.next()).getId() == tmpTeam.getId() ){
					teamExists = true;
				}
			}      	
			if (!teamExists) { TeamList.add(tmpTeam); }
		  }
   	
		  rs.beforeFirst();
      
		  //Alle Gast-Mannschaften einlesen
		  while (rs.next()) {
		boolean teamExists = false;   	
		Team tmpTeam = new Team(rs.getInt("guest"));
   		
		for (Iterator it = TeamList.iterator(); it.hasNext();){      	  
				if ( ((Team)it.next()).getId() == tmpTeam.getId() ){
					teamExists = true;
				}
			}      	
			if (!teamExists) { TeamList.add(tmpTeam); }
		  }      
            
		  for (Iterator it = TeamList.iterator(); it.hasNext();){      	 
			  Team tmpTeam = (Team)it.next();
      	  
			  String sel = 	"SELECT home_score, guest_score FROM matches where tgroup='" + group + 
					"' AND match_type = 0 AND home = " + tmpTeam.getId();
			  java.sql.ResultSet tmpRs = null; //openrs ( stat1, sel );
      	  
			  while (tmpRs.next()) {      	  	
				if (true) {
				//if ( ! (isEmpty(tmpRs.getString("home_score")) || isEmpty(tmpRs.getString("home_score"))) ){
					tmpTeam.addGoals(tmpRs.getInt("home_score"));
					tmpTeam.addGoals(-tmpRs.getInt("guest_score"));
					if (tmpRs.getInt("home_score") > tmpRs.getInt("guest_score")) {
						tmpTeam.addPoints(3);
					}
					if (tmpRs.getInt("home_score") == tmpRs.getInt("guest_score")) {
						tmpTeam.addPoints(1);
					}
				}      	  	
			  }   	  
		  }
      
		  for (Iterator it = TeamList.iterator(); it.hasNext();){      	 
			  Team tmpTeam = (Team)it.next();
      	  
			  String sel = 	"SELECT home_score, guest_score FROM matches where tgroup='" + group + 
					"' AND match_type = 0 AND guest = " + tmpTeam.getId();
			  java.sql.ResultSet tmpRs = null; //openrs ( stat1, sel );
      	  
			  while (tmpRs.next()) {
				if (true) {
				//if ( ! (isEmpty(tmpRs.getString("home_score")) || isEmpty(tmpRs.getString("home_score"))) ){     	  	
					tmpTeam.addGoals(tmpRs.getInt("guest_score"));
					tmpTeam.addGoals(-tmpRs.getInt("home_score"));
					if (tmpRs.getInt("guest_score") > tmpRs.getInt("home_score")) {
						tmpTeam.addPoints(3);
					}
					if (tmpRs.getInt("home_score") == tmpRs.getInt("guest_score")) {
						tmpTeam.addPoints(1);
					}
				}      	  	
			  }   	  
		  }
      
		  Collections.sort(TeamList, Collections.reverseOrder());      
      
		  String res = "";
		  res += "&nbsp<br><br>";
		  res += "<table width=\"50%\" bgcolor=\"#000000\" cellspacing=\"2\" cellpadding=\"5\">";
		  res += "<tr><td style=\"background-color: #ffcc66; text-align: Center\" colspan=\"4\"><font style=\"font-size: 12pt; font-weight: bold\">Gruppe ";
		  res += group + "</font></td></tr>";
		  res += "<tr>";
		  res += "<td width=\"100\" style=\"background-color: #ffcc66; text-align: Center\"><font style=\"font-size: 12pt; font-weight: bold\">Platz</font></td>";
		  res += "<td style=\"background-color: #ffcc66; text-align: Center\"><font style=\"font-size: 12pt; font-weight: bold\">Team</font></td>";
		  res += "<td width=\"100\" style=\"background-color: #ffcc66; text-align: Center\"><font style=\"font-size: 12pt; font-weight: bold\">Punkte</font></td>";
		  res += "<td width=\"100\" style=\"background-color: #ffcc66; text-align: Center\"><font style=\"font-size: 12pt; font-weight: bold\">Tordifferenz</font></td>";      	      
      
		  int i = 1;
		  for (Iterator it = TeamList.iterator(); it.hasNext();){      	
			Team tmp = (Team)it.next(); 
			String teamName = ""; //dLookUp(stat1, "teams", "name", "team_id=" + new Integer(tmp.getId()).toString());
			res += "<tr>";
			res += "<td style=\"background-color: #ffffff; text-align: Center\"><font style=\"font-size: 10pt; font-weight: bold\">";
			res += new Integer(i++).toString() + "</font></td>";
			res += "<td style=\"background-color: #ffffff\"><font style=\"font-size: 10pt; font-weight: bold\">";
			res += "<img src=\"images/"+ teamName +".gif\">   " + teamName + "</font></td>";
			res += "<td style=\"background-color: #ffffff; text-align: Center\"><font style=\"font-size: 10pt; font-weight: bold\">";
			res += new Integer(tmp.getPoints()).toString() + "</font></td>";
			res += "<td style=\"background-color: #ffffff; text-align: Center\"><font style=\"font-size: 10pt; font-weight: bold\">";
			res += new Integer(tmp.getGoalDiff()).toString() + "</font></td>";
		  }      
		  res += "</tr></table>";
      
		  rs.close();
		  stat1.close();
		  conn1.close();
		  return res;
		}
		catch (Exception e) {
		  return e.toString();
		}
	  }


}
